//
//  IncomeViewController.m
//  QianJinDai
//
//  Created by wangzijian on 15/10/19.
//  Copyright (c) 2015年 xuxuan. All rights reserved.
//
#import "ThemeInfoViewController.h"
#import "ThemeFBCell.h"
#import "NSString+WPAttributedMarkup.h"
#import "WPAttributedStyleAction.h"
#import "WPHotspotLabel.h"

#import "SJAvatarBrowser.h"
#import "UIImageView+WebCache.h"
@interface ThemeInfoViewController ()



@end

@implementation ThemeInfoViewController{
    AFHTTPRequestOperationManager *_manager;
    float allHeight;
}
-(void)sendheightData:(float)data{
    CGRect temp =_tableView.frame;
    //            // 2. 给这个变量赋值。因为变量都是L-Value，可以被赋值
        temp.size.height = data;
    //            // 3. 修改frame的值
        _tableView.frame = temp;
    self.tableHeight.constant =data;
    self.sViewHeight.constant =self.tableHeight.constant+self.hViewHeight.constant+10;
}
- (id)init{
    self = [super init];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:NO];
    self.navigationController.navigationBarHidden=NO;
}

- (IBAction)dianzanAction{}
- (void)viewDidLoad
{
    [self initTableData];
    

    _manager = [AFHTTPRequestOperationManager manager];
    self.navigationItem.title=@"话题详情";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor=WhiteColor;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    [self.tableView initData];
    [self.tableView registerClass:[ThemeFBCell class] forCellReuseIdentifier:@"ThemeFBCell"];
    self.tableView.backgroundColor=BackGray;
    self.tableView.heightDataDelegate=self;
    [super viewDidLoad];
     // self.tableView.userInteractionEnabled = NO;
    // Do any additional setup after loading the view from its nib.
}
-(void)initData{
    NSDictionary *d = _fbArray[1];
    _name.text=d[@"name"];
    _time.text =d[@"collection_date"];
    NSString *headurl = [NSString stringWithFormat:@"%@%@",BaseImageURL,[d valueForKey:@"head"]];
    [_head sd_setImageWithURL:[NSURL URLWithString:headurl]];
    if (_head.image==nil) {
        [_head setImage:[UIImage imageNamed:@"head"]];
    }
    //文本内容高度
    NSString * s =[d valueForKey:@"path"];
    _content.text = [d valueForKey:@"content"];
    NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSLineBreakByWordWrapping;
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:14], NSParagraphStyleAttributeName: paragraph};
    CGSize size = [_content.text boundingRectWithSize:CGSizeMake(290, 9999) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute context:nil].size;
        _contentHeight.constant = size.height;

    NSNumber *type = d[@"collection_type"];
    //文本下方图片
    _contentImageView.hidden = YES;
    _hViewHeight.constant = 130+_contentHeight.constant;
    if ([type isEqualToNumber:@0]){
        if (![s isEqualToString:@""]) {
            //图片路径
            NSArray *imgArray =[s componentsSeparatedByString:@","];
            if (imgArray.count<=3) {
                for (int i=0; i<imgArray.count; i++) {
                    UIImageView *cimg = [[UIImageView alloc] initWithFrame:CGRectMake(i*(100+10), 0, 90, 90)];
                    NSString *str = imgArray[i];
                    NSString *imageurl = [NSString stringWithFormat:@"%@%@",BaseImageURL,str];
                    [cimg sd_setImageWithURL:[NSURL URLWithString:imageurl]];
                    if (cimg.gestureRecognizers.count == 0) {
                        cimg.userInteractionEnabled = YES;
                        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
                        [cimg addGestureRecognizer:tap];
                    }
                    [_contentImageView addSubview:cimg];
                }
                _imageHeight.constant=100;
                _hViewHeight.constant= 130+_contentHeight.constant+100;
                _contentImageView.hidden = NO;
            }else if (imgArray.count>3&&imgArray.count<=6){
                for (int i=0; i<3; i++) {
                    UIImageView *cimg = [[UIImageView alloc] initWithFrame:CGRectMake(i*(100+10), 0, 90, 90)];
                    NSString *str = imgArray[i];
                    NSString *imageurl = [NSString stringWithFormat:@"%@%@",BaseImageURL,str];
                    [cimg sd_setImageWithURL:[NSURL URLWithString:imageurl]];
                    if (cimg.gestureRecognizers.count == 0) {
                        cimg.userInteractionEnabled = YES;
                        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
                        [cimg addGestureRecognizer:tap];
                    }
                    [_contentImageView addSubview:cimg];
                }
                for (int i=3; i<imgArray.count; i++) {
                    UIImageView *cimg = [[UIImageView alloc] initWithFrame:CGRectMake((i-3)*(100+10), 100, 90, 90)];
                    NSString *str = imgArray[i];
                    cimg.tag = 3000;
                    
                    NSString *imageurl = [NSString stringWithFormat:@"%@%@",BaseImageURL,str];
                    [cimg sd_setImageWithURL:[NSURL URLWithString:imageurl]];
                    if (cimg.gestureRecognizers.count == 0) {
                        cimg.userInteractionEnabled = YES;
                        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
                        [cimg addGestureRecognizer:tap];
                    }
                    [_contentImageView addSubview:cimg];
                }
                _imageHeight.constant=200;
                _hViewHeight.constant = 130+_contentHeight.constant+200;
                _contentImageView.hidden = NO;
            }else if (imgArray.count>6){
                for (int i=0; i<3; i++) {
                    UIImageView *cimg = [[UIImageView alloc] initWithFrame:CGRectMake(i*(100+10), 0, 90, 90)];
                    NSString *str = imgArray[i];
                    cimg.tag = 3000;
                    
                    NSString *imageurl = [NSString stringWithFormat:@"%@%@",BaseImageURL,str];
                    [cimg sd_setImageWithURL:[NSURL URLWithString:imageurl]];
                    if (cimg.gestureRecognizers.count == 0) {
                        cimg.userInteractionEnabled = YES;
                        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
                        [cimg addGestureRecognizer:tap];
                    }
                    [_contentImageView addSubview:cimg];
                }
                
                for (int i=3; i<6; i++) {
                    UIImageView *cimg = [[UIImageView alloc] initWithFrame:CGRectMake((i-3)*(100+10), 100, 90, 90)];
                    NSString *str = imgArray[i];
                    
                    NSString *imageurl = [NSString stringWithFormat:@"%@%@",BaseImageURL,str];
                    [cimg sd_setImageWithURL:[NSURL URLWithString:imageurl]];
                    if (cimg.gestureRecognizers.count == 0) {
                        cimg.userInteractionEnabled = YES;
                        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
                        [cimg addGestureRecognizer:tap];
                    }
                    [_contentImageView addSubview:cimg];
                }
                for (int i=6; i<imgArray.count; i++) {
                    UIImageView *cimg = [[UIImageView alloc] initWithFrame:CGRectMake((i-6)*(100+10), 200, 90, 90)];
                    
                    NSString *str = imgArray[i];
                    NSString *imageurl = [NSString stringWithFormat:@"%@%@",BaseImageURL,str];
                    [cimg sd_setImageWithURL:[NSURL URLWithString:imageurl]];
                    if (cimg.gestureRecognizers.count == 0) {
                        cimg.userInteractionEnabled = YES;
                        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
                        [cimg addGestureRecognizer:tap];
                    }
                    [_contentImageView addSubview:cimg];
                }
                _imageHeight.constant=300;
                _hViewHeight.constant = 130+_contentHeight.constant+300;
                _contentImageView.hidden = NO;
            }
        }else{
            _hViewHeight.constant = 130+_contentHeight.constant;
        }
        
    }
     
}
//  父类中已经添加了tableview的代理
-(void)initTableData{
    _fbArray = [[NSMutableArray alloc] init];
    _manager = [AFHTTPRequestOperationManager manager];
    //  NSString *url=[NSString stringWithFormat:@"%@chat/getCollection",BaseURL];
    NSDictionary *parameters = @{@"memberid":@17,@"size":@"20",@"endid":@"0"};
    AFHTTPRequestOperation*op= [_manager POST:@"http://testapp.1000fishing.com/api/chat/getCollection"  parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError*error;
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        NSData *jsonData = [[dic valueForKey:@"obj"] dataUsingEncoding:NSUTF8StringEncoding];
        NSArray *result  = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
        NSLog(@"%@",dic);
        if (error==nil&&![result isEqual:[NSNull null]]&&result!=nil&&result.count!=0) {
            for (int i = 0; i<[result count]; i++) {
                NSDictionary *c= result[i];
                [_fbArray addObject:c];
            }
        }
        [self initData];
       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [[[UIAlertView alloc] initWithTitle: @"网络异常" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil] show];
    }];
    op.responseSerializer = [AFCompoundResponseSerializer serializer];
    [op start];
}


@end